﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using DbControl;
using Models;
namespace CentralMonitoring
{
    /// <summary>
    /// 预警窗体
    /// </summary>
    public partial class AlarmForm : Form
    {
        public string mdeviceName;     
        public AlarmForm()
        {
            InitializeComponent();
        }

        private void buttonFind_Click(object sender, EventArgs e)
        {

            RefreshGrid();
        }
        private void InitCombox()
        {
            DataTable dt = OprDeviceConfig.GetDeviceBySensors();
            if (dt != null)
            {
                comboBoxDevice.DisplayMember = "device_name";
                comboBoxDevice.ValueMember = "device_code";
                comboBoxDevice.DataSource = dt;
                comboBoxDevice.SelectedIndex = 0;
            }            
            if (comboBoxDevice.Items.Count > 0)
            {
                if (!string.IsNullOrEmpty(mdeviceName))
                {
                    comboBoxDevice.Text = mdeviceName;
                }
                else
                {
                    comboBoxDevice.SelectedIndex = 0;
                }
            }


            RefreshGrid();
        }
        private void RefreshGrid()
        {
            string filter = "";
            if (!string.IsNullOrEmpty(comboBoxDevice.Text))
            {
                filter = comboBoxDevice.Text;
            }

            DataTable ds = OprWarndatas.GeAlarmsByName(dateTimePicker开始时间.Value, dateTimePicker终止时间.Value,filter);
            if (ds != null)
            {
                this.dataGridView1.DataSource = ds;
                dataGridView1.Columns["id"].Visible = false;
                dataGridView1.Columns["vis40"].Visible = false;
                dataGridView1.Columns["warn_time"].HeaderText = "时间";
                dataGridView1.Columns["warn_time"].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
                dataGridView1.Columns["warn_time"].Width = 140;
                dataGridView1.Columns["vis"].HeaderText = "粘度";
                dataGridView1.Columns["temper"].HeaderText = "温度";
                dataGridView1.Columns["device_name"].HeaderText = "设备名称";
                dataGridView1.Columns["device_code"].HeaderText = "设备编码";
                dataGridView1.Columns["sensor_sn"].HeaderText = "终端编码";
                dataGridView1.Columns["dens"].HeaderText = "密度";
                dataGridView1.Columns["diel"].HeaderText = "介电常数";
                dataGridView1.Columns["ppm"].HeaderText = "含水量";
                dataGridView1.Columns["mo"].Visible = false;
                dataGridView1.Columns["parameter_code"].HeaderText = "预警项";
                dataGridView1.Columns["statu"].HeaderText = "预警标志";
                dataGridView1.Columns["aw"].HeaderText = "水活性";
                dataGridView1.Columns["fe"].HeaderText = "铁磁颗粒";
                dataGridView1.Columns["nfe"].HeaderText = "非铁磁颗粒";
                dataGridView1.Columns["flag"].Visible = false;                        
                dataGridView1.Columns["iso"].Visible = false;
                dataGridView1.Columns["dens"].Visible = false;
                dataGridView1.Columns["vis"].Visible = false;
                dgSetView.SetFormatDataGridView(dataGridView1);
            }

        }

        private void AlarmForm_Load(object sender, EventArgs e)
        {
            dateTimePicker开始时间.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00"));
            dateTimePicker终止时间.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));            
            this.StatusLabel0.Text = string.Format("系统时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            this.WindowState = FormWindowState.Maximized;
            InitCombox();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            StatusLabel0.Text = string.Format("系统时间：{0}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                int code = ExportToExcel.Export(this.dataGridView1);
                switch (code)
                {
                    case -1: MessageBox.Show("保存EXCEL失败，详细原因查看日志(Log)文件"); break;
                    case 1: MessageBox.Show(this, "保存EXCEL成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); break;
                    case 0: break;
                    default: break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据导出失败:" + ex.ToString());
            }
        }
    }
}
